﻿using CBS.APP;
using CBS.DB.CBSEntity;
using CBS.Model.UIModel;
using CBS.Model.ViewModel;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CBS.Desktop.FormPage
{
    /// <summary>
    /// AddStudentSignUp.xaml 的交互逻辑
    /// </summary>
    public partial class AddStudentSignUp : Window
    {
        public AddStudentSignUp()
        {
            InitializeComponent();

            BindSemester();
            BindIntroduceStudent();
        }

        private async void BindSemester()
        {
            try
            {
                var list = await Task.Run(() => SemesterApp.GetSelectList());

                if (list.Count == 0) return;

                this.Dispatcher.Invoke(new Action(() =>
                {
                    cb_Semester.ItemsSource = list;
                }));
                cb_Semester.SelectedIndex = 0;

            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private async void BindCoursePlan()
        {
            try
            {
                int semesterId = ((SemesterSelectModel)cb_Semester.SelectedItem).SemesterId;
                var list = await Task.Run(() => CoursePlanApp.GetFullDataSelectList(semesterId));

                this.Dispatcher.Invoke(new Action(() =>
                {
                    cb_CoursePlan.ItemsSource = list;
                }));
                cb_CoursePlan.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private async void BindIntroduceStudent()
        {
            try
            {
                var list = await Task.Run(() => StudentApp.GetSelectList());
                list.Insert(0, new StudentSelectModel { StudentId = 0, StudentName = "无" });

                this.Dispatcher.Invoke(new Action(() =>
                {
                    cb_Introduce.ItemsSource = list;
                }));
                cb_Introduce.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private void cb_Semester_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            BindCoursePlan();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(tb_StudentName.Text.Trim())) return;

            StudentSignUpViewModel studentSignUp = new();
            studentSignUp.SemesterId = ((SemesterSelectModel)cb_Semester.SelectedItem).SemesterId;
            studentSignUp.CoursePlanId = ((CoursePlanSelectModel)cb_CoursePlan.SelectedItem).CoursePlanId;
            studentSignUp.IntroduceStudentId = ((StudentSelectModel)cb_Introduce.SelectedItem).StudentId;
            studentSignUp.StudentName = tb_StudentName.Text.Trim();
            studentSignUp.PhoneNumber = tb_PhoneNumber.Text.Trim();
            studentSignUp.WxCode = tb_WxCode.Text.Trim();

            try
            {
                string msg = string.Empty;
                if (StudentSignUpApp.AddStudentSignUp(studentSignUp, out msg))
                {
                    this.DialogResult = true;
                }
                else
                {

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }


        private void tb_StudentName_LostFocus(object sender, RoutedEventArgs e)
        {

            string studentName = tb_StudentName.Text.Trim();
            if (string.IsNullOrWhiteSpace(studentName)) return;

            try
            {
                int semesterId = ((SemesterSelectModel)cb_Semester.SelectedItem).SemesterId;
                int coursePlanId = ((CoursePlanSelectModel)cb_CoursePlan.SelectedItem).CoursePlanId;

                if (StudentSignUpApp.CheckDoubleSignUp(coursePlanId, studentName))
                {
                    MessageBox.Show("该学生已报到。", "数据验证", MessageBoxButton.OK, MessageBoxImage.Warning);
                    tb_StudentName.Text = "";
                }
                else
                {
                    if (!StudentSignUpApp.CheckStudentName(studentName))
                    {
                        tb_StudentName.Background = Brushes.LightGreen;
                    }
                    else
                    {
                        tb_StudentName.Background = Brushes.White;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }


        }
    }
}
